
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class zhuaqu {
    @SuppressWarnings("resource")
	public static void main(String[] args) throws Exception {
    	Scanner sc = new Scanner(System.in);
    	System.out.println("请输入需要抓取的网址：");
    	String input = sc.nextLine();
        List<Ticket> list = docatch(input);
        System.out.println(list);
    }
    private static List<Ticket> docatch(String input) {
        List<Ticket> list = new ArrayList<Ticket>();
    	try{
        	//目的网页URL地址
        	URL url = new URL(input);
	        HttpURLConnection httpUrl = (HttpURLConnection)url.openConnection();
	        httpUrl.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
	        InputStream is = httpUrl.getInputStream();
	        BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
	        StringBuilder sb = new StringBuilder();
	        String line;
	        while ((line = br.readLine()) != null) {
	            //这里是对样式进行处理
	            line = line.replaceAll("<(\\w+)[^>]*>", "<$1>");
	            sb.append(line);
	        }
	        is.close();
	        br.close();
	        String[] info = sb.toString().trim().split("</tr>");
	        for (String s : info) {
	        	/*System.out.println(s);
	        	System.out.println("***********************************");*/
	            Matcher m = ticNum.matcher(s);
	            Ticket t = null;
	            if (m.find()) {
	                t = new Ticket();
	                //设置小组名称
	                String name = m.group(1).trim().replace(" ", "");
	                Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
	                Matcher m_html = p_html.matcher(name);
	                name = m_html.replaceAll(""); // 过滤html标签
	                t.setTicName(name);
	                //设置获得票数
	                String ticNum = m.group(2).trim().replace(" ", "").replaceAll("票", "");
	                Pattern p_html1 = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
	                Matcher m_html1 = p_html.matcher(ticNum);
	                ticNum = m_html1.replaceAll(""); // 过滤html标签
	            	t.setTicNum(ticNum);
	                list.add(t);
	            }
	        }
//	        System.out.println("请输入需要查询的队伍数量：");
//	        int length = sc.nextInt();
//	        if(length > list.size()){
//	        	System.out.println("数量超过全部队伍数("+list.size()+")，请重新输入");
//	        	length = sc.nextInt();
//	        }
	        for (int i = 0; i < list.size(); i++) {
	            System.out.println(list.get(i).getTicName()+"\t"+list.get(i).getTicNum());
	        }
	        return list;
        }catch(Exception e){
        	System.out.println("网址输入错误，请重新输入");
        	return null;
        }

	}
	static String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
    static String regex = "<div>\\s*<span>(.*?)</span>\\s*</div>.*<div>\\s*<span>(.*?)</span>\\s*</div>";
    static Pattern ticNum = Pattern.compile(regex, Pattern.DOTALL);
    
}

class Ticket {
    private String ticName;//小组名称
    private String ticNum;//获得票数
	public String getTicName() {
		return ticName;
	}
	public void setTicName(String ticName) {
		this.ticName = ticName;
	}
	public String getTicNum() {
		return ticNum;
	}
	public void setTicNum(String ticNum) {
		this.ticNum = ticNum;
	}
	@Override
	public String toString() {
		return "Ticket [ticName=" + ticName + ", ticNum=" + ticNum + "]";
	}
    
}